package com.gly091020.GLYCan.client;

import com.gly091020.GLYCan.GLYCanMod;
import com.gly091020.GLYCan.block.CanBlock;
import com.gly091020.GLYCan.block.entity.CanBlockEntity;
import com.gly091020.GLYCan.util.CanComponent;
import com.gly091020.GLYCan.util.CanItemContainer;
import com.mojang.datafixers.util.Either;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.network.chat.Style;
import net.minecraft.world.item.ItemStack;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.neoforge.client.event.RegisterColorHandlersEvent;
import net.neoforged.neoforge.client.event.RenderTooltipEvent;
import net.neoforged.neoforge.client.extensions.common.RegisterClientExtensionsEvent;

@EventBusSubscriber(value = Dist.CLIENT, modid = GLYCanMod.ModID)
class EventHandler{
    private static final CanItemExtensions extension = new CanItemExtensions();
    @SubscribeEvent
    public static void onClientSetup(FMLClientSetupEvent event) {
        event.enqueueWork(() -> ItemBlockRenderTypes.setRenderLayer(GLYCanMod.CAN_BLOCK.get(), RenderType.cutout()));
        BlockEntityRenderers.register(GLYCanMod.CAN_BLOCK_ENTITY.get(), context -> new CanBlockEntityRenderer());
    }

    @SubscribeEvent
    public static void registerClientExtensions(RegisterClientExtensionsEvent event) {
        event.registerItem(extension, GLYCanMod.CAN);
    }

    @SubscribeEvent
    public static void registerBlockColorHandlers(RegisterColorHandlersEvent.Block event) {
        event.register((state, level, pos, tintIndex) -> {
            if(level != null && pos != null){
                var b = level.getBlockEntity(pos);
                if(b instanceof CanBlockEntity entity){
                    return entity.color;
                }
            }
            return CanBlock.NORMAL_COLOR;
        }, GLYCanMod.CAN_BLOCK.get());
    }

    @SubscribeEvent
    public static void registerItemColorHandlers(RegisterColorHandlersEvent.Item event) {
        event.register((itemStack, tintIndex) -> {
            if(itemStack.is(GLYCanMod.CAN)){
                var p = itemStack.getOrDefault(GLYCanMod.CAN_COMPONENTS, new CanComponent(false, CanBlock.NORMAL_COLOR));
                if(p.isTeto()){
                    return CanBlock.NORMAL_COLOR;
                }else{
                    return p.color();
                }
            }
            return CanBlock.NORMAL_COLOR;
        }, GLYCanMod.CAN.get());
    }

    @SubscribeEvent
    public static void registryTooltip(RenderTooltipEvent.GatherComponents event){
        if (event.getItemStack().is(GLYCanMod.CAN)){
            var c = event.getItemStack().get(GLYCanMod.CAN_COMPONENTS);
            if(c != null) {
                if(c.color() != CanBlock.NORMAL_COLOR || c.isTeto()){
                    if (c.isTeto()) {
                        event.getTooltipElements().add(
                                Either.left(FormattedText.composite(FormattedText.of(Component.translatable("tip.gly_can.can.1",
                                        Component.translatable("tip.gly_can.can.teto")).getString()))
                                ));
                    } else {
                        event.getTooltipElements().add(
                                Either.left(FormattedText.composite(FormattedText.of(Component.translatable("tip.gly_can.can.1",
                                        String.format("#%x", c.color()).toUpperCase()).getString()), FormattedText.of("■",
                                        Style.EMPTY.withColor(c.color()))))
                        );
                    }
                }
                var l = new CanItemContainer(event.getItemStack()).getMaxItems();
                if(!l.isEmpty()){
                    event.getTooltipElements().add(Either.left(FormattedText.of(Component.translatable("tip.gly_can.can.2").getString())));
                    for(ItemStack stack: l){
                        if(!stack.isEmpty()){
                            event.getTooltipElements().add(Either.left(FormattedText.composite(
                                    FormattedText.of("- " + stack.getHoverName().getString()),
                                    FormattedText.of(String.format(" x%d", stack.getCount()))
                            )));
                        }
                    }
                }
            }
        }
    }
}

@Mod(dist = Dist.CLIENT, value = GLYCanMod.ModID)
public class GLYCanClient {

}
